package com.gimi.cloud.jimi.task.service.impl;

import java.util.List;

import com.gimi.cloud.jimi.commonService.InterfacePlatform.InterfacePlatformRequestService;
import com.gimi.cloud.jimi.dao.dao.moreInfo.CreditcardBankcodeDao;
import com.gimi.cloud.jimi.dao.dao.sys.SysConfigDao;
import com.gimi.cloud.jimi.entity.app.moreInfo.CreditcardBankcodeEntity;
import com.gimi.cloud.jimi.task.service.FengChaoCreditcardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/**
 * 蜂巢信用卡服务
 *
 */
@Service
public class FengChaoCreditcardServiceImpl implements FengChaoCreditcardService {

	private static final Logger log = LoggerFactory.getLogger(FengChaoCreditcardServiceImpl.class);
	private final static String INTERFACE_PLATFORM_UNIVERSAL_CREDITCARDWATER = "interface_platform_universal_creditCardWater";

	private CreditcardBankcodeDao creditcardBankcodeDao;
	private InterfacePlatformRequestService requestService;
	private SysConfigDao sysConfigDao;
    @Autowired
	public FengChaoCreditcardServiceImpl(CreditcardBankcodeDao creditcardBankcodeDao, InterfacePlatformRequestService requestService, SysConfigDao sysConfigDao) {
		this.creditcardBankcodeDao = creditcardBankcodeDao;
		this.requestService = requestService;
		this.sysConfigDao = sysConfigDao;
	}

	@Override
	public void getSupportedBanks() {
		log.info("开始获取信用卡银行相关参数");
		
		log.info("读取配置表key="+INTERFACE_PLATFORM_UNIVERSAL_CREDITCARDWATER);
		String fengchaoCreditcard = sysConfigDao.getConfigByKey(INTERFACE_PLATFORM_UNIVERSAL_CREDITCARDWATER);
		JSONObject fcJson = JSON.parseObject(fengchaoCreditcard);
		JSONObject biparam = fcJson.getJSONObject("fengchao_bankonline_getbasicinfo");
		JSONObject body = biparam.getJSONObject("body");
		
		List<CreditcardBankcodeEntity> bankList = creditcardBankcodeDao.selectAll();
		for (CreditcardBankcodeEntity bankcode : bankList) {
			body.put("bankCode", bankcode.getFcbcBankId());
			biparam.put("body", body);
			try {
				String sendApiPost = requestService.sendApiPost(biparam.toJSONString());
				JSONObject bankparam = JSON.parseObject(sendApiPost);
				JSONArray loginTypes = bankparam.getJSONObject("body").getJSONArray("loginTypes");
				bankcode.setFcbcBankLoginparam(loginTypes.toJSONString());
				creditcardBankcodeDao.baseUpdateByPrimaryKey(bankcode);
			} catch (Exception e) {
				log.info("获取银行登录参数错误:"+e.getClass());
				e.printStackTrace();
				continue;
			}
		}
		
		log.info("获取信用卡银行相关参数完成");
	}

}